package cn.livingCloud.controller.core.validator;

import cn.livingCloud.dto.CaseReportFormDto;
import cn.livingCloud.service.CaseService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

/**
 * Created by 唐旭 on 2016/6/6.
 */
@Component
public class CaseReportFormDtoValidator implements Validator {
    @Autowired
    private CaseService caseService;

    @Override
    public boolean supports(Class<?> aClass) {
        return CaseReportFormDto.class.equals(aClass);
    }

    @Override
    public void validate(Object target, Errors errors) {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "caseUuid", null, "");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "content", null, "内容不能为空");
        CaseReportFormDto formDto = (CaseReportFormDto) target;
        String caseUuid = formDto.getCaseUuid();
        if (StringUtils.isNotEmpty(caseUuid)) {
            boolean result = caseService.checkUnModify(caseUuid);
            if (result) {
                errors.rejectValue("content", null, "案件已完成处置,不能进行变更");
            }
        }
    }
}
